<?php


if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Eksternal_Interface_Model extends CI_Model {

    function __construct() {
        parent::__construct();
    }

    function update_status_request($id_request, $response_code, $keterangan) {
        $sql = "update request_transaksi set status=?, keterangan=? where id_request=?";
        $this->db->query($sql, array($response_code, $keterangan, $id_request));
    }
    
    function update_status_new_member($id_member) {
        $sql = "update member set sync_with_lokal=1 where id_member=?";
        $this->db->query($sql, array($id_member));
    }

    function update_status_deposit($id_request, $response_code) {
        $sql = "update deposit set status=? where id_deposit=?";
        $this->db->query($sql, array($response_code, $id_request));
    }
    
    function update_status_outbox($id_request, $response_code) {
        $sql = "update message_outbox set is_sent=?, date_sent=now() where id_message=?";
        $this->db->query($sql, array($response_code, $id_request));
    }

    function request_waiting_transaksi($maksimum_data_per_request = 20) {
        $maksimum_data_per_request = strip_tags(mysql_real_escape_string($maksimum_data_per_request));
        $maksimum_data_per_request = intval($maksimum_data_per_request);

        $sql = "select * from request_transaksi where status = 0 order by id_request limit " . $maksimum_data_per_request;
        $query = $this->db->query($sql);

        if ($query->num_rows() > 0) {
            $resp_data = json_encode($query->result());
            foreach ($query->result() as $row) {
                $this->db->query("update request_transaksi set status = -1 where id_request = ".$row->id_request);
            }
            return $resp_data;
        }

        return "none";
    }

    function request_outbox($maksimum_data_per_request = 20) {
        $maksimum_data_per_request = strip_tags(mysql_real_escape_string($maksimum_data_per_request));
        $maksimum_data_per_request = intval($maksimum_data_per_request);

        $sql = "select * from message_outbox where is_sent = 0 order by id_message limit " . $maksimum_data_per_request;
        $query = $this->db->query($sql);

        if ($query->num_rows() > 0) {
            $resp_data = json_encode($query->result());
            foreach ($query->result() as $row) {
                $this->db->query("update message_outbox set is_sent = -1 where id_message = ".$row->id_message);
            }
            return $resp_data;
        }

        return "none";
    }
    
    function request_deposit($maksimum_data_per_request = 20) {
        $maksimum_data_per_request = strip_tags(mysql_real_escape_string($maksimum_data_per_request));
        $maksimum_data_per_request = intval($maksimum_data_per_request);

        $sql = "select * from deposit where status = 0 order by id_deposit limit " . $maksimum_data_per_request;
        $query = $this->db->query($sql);

        if ($query->num_rows() > 0) {
            $resp_data = json_encode($query->result());
            foreach ($query->result() as $row) {
                $this->db->query("update deposit set status = -1 where id_deposit = ".$row->id_deposit);
            }
            return $resp_data;
        }

        return "none";
    }
    
    function request_new_member($maksimum_data_per_request = 20) {
        $maksimum_data_per_request = strip_tags(mysql_real_escape_string($maksimum_data_per_request));
        $maksimum_data_per_request = intval($maksimum_data_per_request);

        $sql = "select * from member where sync_with_lokal = 0 order by id_member limit " . $maksimum_data_per_request;
        $query = $this->db->query($sql);

        if ($query->num_rows() > 0) {
            $resp_data = json_encode($query->result());
            foreach ($query->result() as $row) {
                $this->db->query("update member set sync_with_lokal = -1 where id_member = ".$row->id_member);
            }
            return $resp_data;
        }

        return "none";
    }

    function flag_queue_message_outbox($id_queue, $id_request, $receiver, $status) {
        if ($status === 0) {
            $sql = "insert into flag_message_outbox(id_queue,id_request,receiver,status) values (?,?,?,?)";
            $this->db->query($sql, array($id_queue, $id_request, $receiver, $status));
        } else {
            $sql = "update flag_message_outbox set status=? where id_request=? and receiver=?";
            $this->db->query($sql, array($status, $id_request, $receiver));
        }
    }

    function sync_local_to_web($part_data) {
        $sql = "insert into log_sync_local_to_web values (?,?,?,?,?)";
        $this->db->query($sql, array($part_data->id_log, $part_data->query, $part_data->data, $part_data->flag, $part_data->time_created));
        
        $sql = $part_data->query;
        $this->db->query($sql, json_decode($part_data->data));
        
        return true;
    }

}